unit NewItemUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Mask, ADODB, DBUnit, DB, rxPlacemnt;

type
  TNewItemForm = class(TForm)
    ComboBox1: TComboBox;
    Label1: TLabel;
    Label2: TLabel;
    MaskEdit1: TMaskEdit;
    Label3: TLabel;
    ComboBox2: TComboBox;
    Label4: TLabel;
    MaskEdit2: TMaskEdit;
    Label5: TLabel;
    ComboBox3: TComboBox;
    StaticText1: TStaticText;
    Button1: TButton;
    Button2: TButton;
    ADOQuery1: TADOQuery;
    ADOQuery2: TADOQuery;
    ADOQuery3: TADOQuery;
    FormStorage1: TFormStorage;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  NewItemForm: TNewItemForm;

function CreateItem(const aOrderId: integer): integer;

implementation

function CreateItem(const aOrderId: integer): integer;
var
  insertADOQuery: TADOQuery;
  showResult: integer;
begin
  if (not Assigned(NewItemForm)) then begin
    NewItemForm:= TNewItemForm.Create(nil);
  end;

  showResult:= NewItemForm.ShowModal;
  if (showResult = mrOk) then begin
  (*
    insertADOQuery:= TADOQuery.Create(nil);
    insertADOQuery.Connection:= DBUnit.DBDataModule.DBADOConnection;
    insertADOQuery.SQL.Clear;
    insertADOQuery.SQL.Add('INSERT INTO Orders(ordernum,clientid,orderdate)');
    insertADOQuery.SQL.Add('VALUES (:aOrderNum,:aClientId,:aOrderDate)');
    insertADOQuery.Parameters.ParamByName('aOrderNum').Value:= NewOrderForm.Edit1.Text;
    insertADOQuery.Parameters.ParamByName('aClientId').Value:= aClientId;
    insertADOQuery.Parameters.ParamByName('aOrderDate').Value:= NewOrderForm.DateTimePicker1.DateTime;
    insertADOQuery.ExecSQL;
    insertADOQuery.SQL.Clear;
    insertADOQuery.SQL.Add('SELECT Orders.* FROM Orders WHERE Orders.clientid=:aClientId ORDER BY Orders.orderid desc');
    insertADOQuery.Parameters.ParamByName('aClientId').Value:= aClientId;
    insertADOQuery.Open;
    insertADOQuery.Active:= True;
    insertADOQuery.First;
    CreateOrder:= insertADOQuery.FieldByName('clientid').AsInteger;
    insertADOQuery.Close;
    insertADOQuery.Active:= False;
    *)
    FreeAndNil(insertADOQuery);
  end;
  FreeAndNil(NewItemForm);
end;

{$R *.dfm}

procedure TNewItemForm.FormCreate(Sender: TObject);
var
  id: integer;
begin
  while (not ADOQuery1.Eof) do begin
    id:= ADOQuery1.FieldByName('objectid').AsInteger;
    ComboBox1.AddItem(ADOQuery1.FieldByName('objectname').AsString,TObject(id));
    ADOQuery1.Next;
  end;

  if (ComboBox1.Items.Count > 0) then begin
    ComboBox1.ItemIndex:= 0;
  end;
end;

end.
